package com.sxrong.carloan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sxrong.carloan.R;
import com.sxrong.carloan.dto.CarInfoBean;
import com.sxrong.carloan.utils.CommonListener;

import java.util.List;

/**
 * Created by chp on 2018/3/22.
 */

public class CarAdapter extends RecyclerView.Adapter<CarAdapter.CarViewHolder> {

    private Context mContext;
    private List<CarInfoBean> list;
    private CommonListener<CarInfoBean> commonListener;

    public CarAdapter(Context context){
        mContext = context;
    }

    public void setCommonListener(CommonListener<CarInfoBean> commonListener) {
        this.commonListener = commonListener;
    }

    @Override
    public CarViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CarViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_car_layout , parent , false));
    }

    @Override
    public void onBindViewHolder(CarViewHolder holder, int position) {
        holder.borrower_name.setText("借款人: " + list.get(position).getBorrower());
        holder.carman_name.setText("车主 :" + list.get(position).getCarman());
        holder.loan_date.setText("借款日期: " + list.get(position).getDate());
        holder.company_name.setText("公司 :" + list.get(position).getCompany());
        holder.car_model.setText("车型: " + list.get(position).getCarModel());
        holder.car_no.setText("车牌号 :" + list.get(position).getCarNo());
        holder.car_color.setText("颜色: "+list.get(position).getColor());
        holder.borrow_amount.setText("借款总额 :" + list.get(position).getBorroweAmount());
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size() ;
    }


    public class CarViewHolder extends RecyclerView.ViewHolder{
        private TextView borrower_name;
        private TextView carman_name;
        private TextView loan_date;
        private TextView company_name;
        private TextView car_model;
        private TextView car_no;
        private TextView car_color;
        private TextView borrow_amount;
        public CarViewHolder(View itemView) {
            super(itemView);
            borrower_name = itemView.findViewById(R.id.borrower_name);
            borrow_amount = itemView.findViewById(R.id.borrow_amount);
            carman_name = itemView.findViewById(R.id.carman_name);
            loan_date = itemView.findViewById(R.id.loan_date);
            company_name = itemView.findViewById(R.id.company_name);
            car_model = itemView.findViewById(R.id.car_model);
            car_no = itemView.findViewById(R.id.car_no);
            car_color = itemView.findViewById(R.id.car_color);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if(commonListener != null){
                        commonListener.onItemClick(position , list.get(position));
                    }
                }
            });
        }

    }

    public void setList(List<CarInfoBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addList(List<CarInfoBean> list){
        if(this.list != null) {
            this.list.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearAll(){
        if(list != null){
            list.clear();
        }
        notifyDataSetChanged();
    }
}
